<template>
  <div>
    <el-table
      size="small"
      :data="tableData"
      @sort-change="shortHandler"
      @selection-change="handleSelectionChange"
      stripe
      border
    >
      <el-table-column
        type="selection"
        align="center"
        width="50px"
      ></el-table-column>
      <el-table-column
        sortable
        prop="serialNumber"
        label="序列号"
        align="center"
      ></el-table-column>
      <el-table-column
        sortable
        prop="name"
        label="设备名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="updatedAt"
        label="更新时间"
        sortable
        align="center"
        width="160px"
      ></el-table-column>
      <el-table-column
        label="操作"
        width="220px"
        align="center"
        v-if="userGuardList.includes(guard.RouterManage)"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="send(scope.row)"
            @refresh="search"
          >
            <svg-icon icon-class="push"></svg-icon> 发送端
          </el-button>
          <el-button
            type="text"
            size="small"
            @click="receive(scope.row)"
          >
            <svg-icon
              icon-class="receive"
              style="color:#66b1ff"
            ></svg-icon> 接收组
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import moment from 'moment';
import { MomentFormat, SortType } from '../../../../constants/common';
import { Message, MessageBox } from 'element-ui';
import { parseError } from '../../../../utils';

export default {
  model: {
    prop: 'sort'
  },
  props: {
    tableData: {
      type: Array,
      default() {
        return [];
      }
    },
    sort: {
      type: Object,
      default() {
        return {
          sortType: SortType.DESC
        };
      }
    }
  },
  data() {
    return {
      selectedDeviceList: [],
      btnLoading: false
    };
  },
  methods: {
    handleSelectionChange(value) {
      this.selectedDeviceList = value.map(item => {
        return {
          uuid: item.uuid,
          name: item.name,
          serialNumber: item.serialNumber
        };
      });
      this.$emit('select', this.selectedDeviceList);
    },
    shortHandler({ order, prop }) {
      this.$emit('orderBy', prop);
      this.sort.sortType = order === 'ascending' ? SortType.ASC : SortType.DESC;
      this.$emit('search');
    },
    send(row) {
      const device = {
        uuid: row.uuid,
        name: row.name,
        serialNumber: row.serialNumber
      };
      this.$emit('send', device);
    },
    receive(row) {
      const device = {
        uuid: row.uuid,
        name: row.name,
        serialNumber: row.serialNumber
      };
      this.$emit('receive', device);
    }
  }
};
</script>
